package com.yzkj.cpjxc.business.controller;

import com.yzkj.cpjxc.business.domain.DictionaryConstants;
import com.yzkj.cpjxc.business.domain.DicObj;
import com.yzkj.cpjxc.business.service.DicObjService;
import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.CSRFTokenManager;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.StringHelp;
import com.yzkj.cpjxc.business.system.interceptor.DoLog;
import com.yzkj.cpjxc.business.system.interceptor.Permission;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * 字典公共控制器
 */
@Controller
@RequestMapping("/dicObj")
public class DicObjController extends BaseController {
	
	@Autowired
	private DicObjService dicObjService;
	
	/**
	 * 去到字典公共管理列表页面
	 */
	@RequestMapping("/list") 
	@DoLog(cnContent="查看字典公共管理列表信息")
	@Permission(name="dicObj.view")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception{
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("dicObj", dicObj);
		
		if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
			//保存到当前回话中，在页面可以取到最后操作的字典组别
			request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
		}
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("dicGroupTypeMap", DictionaryConstants.getDicTypeMap());
		return new ModelAndView("dictionary/dicObj/list").addAllObjects(modelMap);
	}
	
	/**
	 * 字典公共管理列表ajax分页
	 */
	@RequestMapping("/ajaxPage") 
	@DoLog(cnContent="字典公共管理列表ajax分页") 
	@Permission(name="dicObj.view")
	public String ajaxPage(HttpServletRequest request,HttpServletResponse response,DicObj dicObj) throws Exception{
		try {
			
			if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
				//保存到当前回话中，在页面可以取到最后操作的字典组别
				request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
			}
			
			if(StringUtils.isBlank(dicObj.getDicGroupType())){
				return this.ajax(response, "请选择字典组别！");
			}
			
			Pager<DicObj> p = new Pager<DicObj>();
			p.setDicGroupType(dicObj.getDicGroupType());
			boolean exportExcel = BooleanUtils.toBoolean(request.getParameter(EXPORT_EXCEL));
			if (exportExcel) {
				return null;
			}else {
				String currentPage = request.getParameter(Pager.MY_PAGINATION_CURRENT_PAGE);
				p.setCurrentPage(Integer.parseInt(currentPage == null ? "1" : currentPage));
				dicObjService.search(p, dicObj);
				this.genPageJson(response, p);
				return null;
			}
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}
	
	
	/**
	 * 去到添加字典公共管理页面
	 */
	@RequestMapping("/toAdd") 
	@DoLog(cnContent="去到添加字典公共管理页面") 
	@Permission(name="dicObj.add")
	public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("dicGroupTypeMap", DictionaryConstants.getDicTypeMap());
		if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
			//保存到当前回话中，在页面可以取到最后操作的字典组别
			request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
		}
		modelMap.put("dicObj", dicObj);
		return new ModelAndView("dictionary/dicObj/add").addAllObjects(modelMap);
	}

	/**
	 * 添加字典公共管理
	 */
	@RequestMapping("/add") 
	@DoLog(cnContent="添加字典公共管理", operationAction = "C") 
	@Permission(name="dicObj.add")
	public String add(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		try {
			
			//过滤指定非法字符
			StringHelp.replaceFilteringChars(DicObj.class, dicObj);
			
			if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
				//保存到当前回话中，在页面可以取到最后操作的字典组别
				request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
			}
			
			if(StringUtils.isBlank(dicObj.getDicGroupType())){
				return this.ajax(response, "字典组别不能为空！");
			}else{
				String dicGroupName = DictionaryConstants.getDicTypeMap().get(dicObj.getDicGroupType());
				if(dicGroupName==null){
					return this.ajax(response, "字典组别不存在！");
				}else{
					dicObj.setDicGroupName(dicGroupName);
				}
			}
			
			BusinessMap bm = dicObjService.create(dicObj);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}
	
	/**
	 * 去到编辑字典公共管理页面
	 */
	@RequestMapping("/toEdit") 
	@DoLog(cnContent="去到编辑字典公共管理页面") 
	@Permission(name="dicObj.edit")
	public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		DicObj result = dicObjService.getById(dicObj.getUuid());
		HashMap<String, Object> modelMap=new LinkedHashMap<String, Object>();
		modelMap.put("csrfToken",  CSRFTokenManager.getTokenForSession(request.getSession()));
		modelMap.put("dicGroupTypeMap", DictionaryConstants.getDicTypeMap());
		
		if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
			//保存到当前回话中，在页面可以取到最后操作的字典组别
			request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
		}
		
		modelMap.put("dicObj",result);
		return new ModelAndView("dictionary/dicObj/edit").addAllObjects(modelMap);
	}
	
	/**
	 * 编辑字典公共管理
	 */
	@RequestMapping("/edit") 
	@DoLog(cnContent="编辑字典公共管理", operationAction = "U") 
	@Permission(name="dicObj.edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		try {
			
			//过滤指定非法字符
			StringHelp.replaceFilteringChars(DicObj.class, dicObj);
			
			if(StringUtils.isNotBlank(dicObj.getDicGroupType())){
				//保存到当前回话中，在页面可以取到最后操作的字典组别
				request.getSession().setAttribute("dicGroupType", dicObj.getDicGroupType());
			}
			
			if(StringUtils.isBlank(dicObj.getDicGroupType())){
				return this.ajax(response, "字典组别不能为空！");
			}else{
				String dicGroupName = DictionaryConstants.getDicTypeMap().get(dicObj.getDicGroupType());
				if(dicGroupName==null){
					return this.ajax(response, "字典组别不存在！");
				}else{
					dicObj.setDicGroupName(dicGroupName);
				}
			}
			
			BusinessMap bm = dicObjService.edit(dicObj);
			return this.ajax(response, bm.getIsSucc() ? Constants.SUCCESS : bm.getAlertMesg());
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}
	
	/**
	 * 查看字典公共管理信息
	 */
	@RequestMapping("/view") 
	@DoLog(cnContent="查看字典公共管理信息") 
	@Permission(name="dicObj.view")
	public ModelAndView view(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		DicObj result = dicObjService.getById(dicObj.getUuid());
		return new ModelAndView("dictionary/dicObj/view").addObject("dicObj",result);
	}
	
	/**
	 * 删除字典公共管理信息
	 */
	@RequestMapping("/delete") 
	@DoLog(cnContent="删除字典公共管理信息", operationAction = "D") 
	@Permission(name="dicObj.delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, DicObj dicObj) throws Exception {
		try {
			boolean flag = dicObjService.delete(dicObj.getUuid());
			return this.ajax(response, flag ? Constants.SUCCESS : Constants.ERROR);
		} catch (Exception e) {
			this.logException(e);
			return this.ajax(response, "系统出现异常: [" + e.getMessage() + "] 请与管理员联系！");
		}
	}
	
}
